﻿CREATE PROCEDURE [dbo].[DropDatabase]
	@dbName NVARCHAR(200)
AS
BEGIN
	DECLARE @query varchar(2000)
	IF EXISTS(SELECT * FROM [master].[dbo].[sysdatabases] WHERE name = @dbName)
	BEGIN
			SET @query = 'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N''' + @dbName + ''''
			EXEC (@query)

			EXEC [dbo].[CloseExistsDbConnection] @dbName = @dbName

			SET @query = 'DROP DATABASE [' + @dbName + ']'
			EXEC (@query)
	END
END